//
//  GRHoMeCollectionView.m
//  YC_GRice
//
//  Created by Feng Liu on 2018/9/26.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRHoMeCollectionView.h"
#import "GRUltimateShopViewCell.h"
#import "GRMoreShopReusableView.h"
#import "GRPayBillViewController.h"
#import "GRUltimateHomeViewController.h"

@interface GRHoMeCollectionView () <GRMoreShopReusableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
    CGFloat referenceHeight;
    CGFloat itemWidth, itemHeight;
}
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) GRUltimateModel *nearbyShopModel;

@end

@implementation GRHoMeCollectionView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self drawUI];
    }
    return self;
}

- (void)drawUI {
    itemWidth         = KscreenWidth - 20;
    itemHeight        = KscreenWidth/3 + 10;
    
    referenceHeight = self.bounds.size.height - itemHeight;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.frame collectionViewLayout:[UICollectionViewFlowLayout new]];
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.delegate     = self;
    self.collectionView.dataSource = self;
    [self addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"GRMoreShopReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"GRMoreShopReusableView"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"GRUltimateShopViewCell" bundle:nil] forCellWithReuseIdentifier:@"GRUltimateShopViewCell"];
}

- (void)reloadDataWithNearbyShops:(GRUltimateModel *)nearbyShopModel {
    self.nearbyShopModel = nearbyShopModel;
    [self.collectionView reloadData];
}

- (void)exchnageItemToFirstWithIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item < self.nearbyShopModel.storeList.count) {
        [self.nearbyShopModel.storeList exchangeObjectAtIndex:indexPath.item withObjectAtIndex:0];
        [self.collectionView reloadItemsAtIndexPaths:@[indexPath, [NSIndexPath indexPathForItem:0 inSection:0]]];
    }
}


#pragma Mark ----------------  UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout  ------------------
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.nearbyShopModel.storeList.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GRUltimateShopViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GRUltimateShopViewCell" forIndexPath:indexPath];
    cell.shopModel                     = self.nearbyShopModel.storeList[indexPath.item];
    return cell;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        GRMoreShopReusableView *headerRv = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"GRMoreShopReusableView" forIndexPath:indexPath];
        headerRv.delegate                          = self;
        return headerRv;
    }
    return [UICollectionReusableView new];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return self.nearbyShopModel.storeList.count > 0 ? CGSizeMake(itemWidth, referenceHeight) : CGSizeZero;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(itemWidth, itemHeight);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0.f;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([GRUserBasisInfo isLogin]) {
        GRUltimateShopModel *shopModel = self.nearbyShopModel.storeList[indexPath.item];
        GRPayBillViewController *billVc    = [[GRPayBillViewController alloc] init];
        billVc.storeId                             = shopModel.profileOnePayId;
        billVc.isValid = YES;
        [self.currentVc.navigationController pushViewController:billVc animated:YES];
    }else {
        [self.currentVc goToLogin];
    }
}

-(void)checkOutMoreNearbyShopAction:(BOOL)display {
    CGFloat offset_y = 0;
    if (display) {
        offset_y = (self.nearbyShopModel.storeList.count * itemHeight) >= (referenceHeight - 40) ? (referenceHeight - 40) : (self.nearbyShopModel.storeList.count - 1) * itemHeight;
    }
    [self.collectionView setContentOffset:CGPointMake(0, offset_y) animated:YES];
}

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *hitView = [super hitTest:point withEvent:event];
    
    if (!self.hidden && [hitView isKindOfClass:[UICollectionReusableView class]] && (referenceHeight - 40) > point.y && self.collectionView.contentOffset.y < (referenceHeight - 40)) {
        if (point.y >= referenceHeight -80 && point.y <= referenceHeight - 40) {
            if (point.x >= KscreenWidth-50 && point.x <= KscreenWidth-10) {
                return [self.superview viewWithTag:101];
            }else  if (point.x >= 10 && point.x <= 50){
                return [self.superview viewWithTag:100];
            }else if (point.x>=((KscreenWidth-110)/2)&&point.x <=(KscreenWidth+110)/2 ){
                return [self.superview viewWithTag:104];
            }
        }else if (point.x >= 10 && point.x <= 50 && point.y >= referenceHeight -120 && point.y <= referenceHeight -80) {
            return [self.superview viewWithTag:102];
        }else if (point.x >= 10 && point.x <= self.currentVc.RiceBalBox.width && point.y >= 0 && point.y <= 45) {
            return [[self.superview viewWithTag:103] hitTest:point withEvent:event];
        }
        return [self.superview viewWithTag:1000];
    }
    
    return hitView;
}



@end
